package com.ofr.controller.common;

import com.ofr.credentials.Credentials;
import com.ofr.ejb.dao.UserDao;
import com.ofr.ejb.entities.User;
import com.ofr.ejb.service.UserService;
import com.ofr.qualifiers.CurrentSession;
import com.ofr.session.UserSession;
import com.ofr.util.Constants;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: imon
 * Date: 3/28/13
 * Time: 12:18 PM
 * To change this template use File | Settings | File Templates.
 */
@Named
@RequestScoped
public class LoginController implements Serializable, Constants {

    @Inject
    Credentials credentials;

    @Inject
    @CurrentSession
    UserSession userSession;

    @EJB
    UserService userService;

    @PostConstruct
    public void init() {
    }

    public String logIn() {
        System.out.println("in Login  method");

        User authenticatedUser = userService.logInService(credentials.getEmail(), credentials.getPassword());

        if (authenticatedUser != null) {
            userSession.setUser(authenticatedUser);
            if (authenticatedUser.getRole().equals(ADMIN_ROLE)) {
                return ADMIN_LOGIN_SUCCESSFUL;
            } else {
                return LOGIN_SUCCESSFUL;
            }
        }
        return ERROR;

    }

    public User getUser(){
        return userSession.getUser();
    }

}